<?php

use App\Exceptions\MemberException;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

// server
if( Request::method() === 'POST' )
{
    $system = trim( Request::getPathInfo(), '/' );    // 服务系统
    $service = Request::post( 'service' );  // 服务名称
    
    // 查询映射关系
    if( $mapping = Config::get( 'mapping.' . $system ) )
    {
        if( isset( $mapping[ $service ] ) )
        {
            // 设置动态路由
            Route::prefix( $system )->middleware( [ 'service.authrization', 'validate' ] )->group( function () use ( $mapping, $service ) {
                Route::post( '', $mapping[ $service ] );
            } );
        }
        
        else
        {
            throw new MemberException( 'service not found', 10005 );
        }
    }
}

// member
Route::prefix( 'member' )->middleware( [ 'validate', 'member.authrization', 'member.permission' ] )->group( function () {
    // user
    // Route::group( [ 'prefix' => 'user', 'namespace' => 'App\Http\Controllers\member\user', 'middleware' => [ 'validate', 'member.authrization', 'member.permission' ] ], function () {
    Route::group( [ 'prefix' => 'user', 'namespace' => 'App\Http\Controllers\member\user', ], function () {
        Route::post( '/login', 'login@index' );   // 登陆
        Route::post( '/register', 'register@index' );   // 注册密码
        Route::post( '/forget', 'forget@index' );   // 找回密码
        
        Route::post( '/', 'index@index' );    // 用户信息
    } );

    // 
} );

// test
Route::prefix( 'test' )->middleware( [ 'validate' ] )->group( function () {
    // index
    Route::group( [ 'prefix' => '/', 'namespace' => 'App\Http\Controllers\test', ], function() {
        Route::post( 'decode_param', 'index@decode_param' );   // 解密参数
        Route::post( 'encode_param/{system}/{service}', 'index@encode_param' );   // 加密参数
        Route::post( 'send/{system}/{service}', 'index@send' );   // 模拟数据发送
        Route::post( 'run', 'index@run' );   // 执行测试
    } );
} );

// 空路由
Route::fallback( function () {
    return response()->json( [ 'code' => 10000, 'message' => 'document not found' ] );
} );